﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.Linq;

using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.DL.Data.Schema;

#endregion

namespace ScrumTable.DL.Data.Generic.Cache
{
    /// <summary>
    /// Represents a workflow wrapper with state management facilities.
    /// </summary>
    public class CacheWorkflow : CacheElementBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly CacheElementList<CacheTransition> _transitions;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the schema field definition with the associated state.
        /// </summary>
        public SchemaBase FieldSchema { get; private set; }

        /// <summary>
        /// Returns true if the current workflow isn't customized by the
        /// underlying schema. So every WorkItem transition must be validated
        /// through the back-end system.
        /// </summary>
        public bool IsStrict { get; private set; }

        /// <summary>
        /// Gets the name (or in some cases like to work item the title) of
        /// the underlying element.
        /// </summary>
        public override string Name
        {
            get { return FieldSchema.SchemaFullName; }
            set { }
        }

        /// <summary>
        /// Specifies the start transition state.
        /// </summary>
        public string TransitionStart { get; private set; }

        /// <summary>
        /// Specifies the end transition state.
        /// </summary>
        public string TransitionEnd { get; private set; }

        /// <summary>
        /// Gets a list of associted workflow cycle transitions (State A -> State B).
        /// </summary>
        public CacheElementList<CacheTransition> Transitions
        {
            get { return _transitions; }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="CacheWorkItem"/> class.
        /// </summary>
        /// <param name="uniqueId">Specifies the unique id of the current workflow.</param>
        /// <param name="backEndTransitions">Specifies the back end workflow transitions.</param>
        /// <param name="schemaTransitions">Specifies a list of transitions which should be filled into the new workflow. These transitions are given by the schema.</param>
        /// <param name="schemaField">Specifies the schema field definition with the associated state.</param>
        /// <param name="isStrictWorkflow">Specifies a boolean which indicates that the workflow is customized and does not follow special back-end system rules.</param>
        /// <param name="backEndStates">Specifies the states of the back-end system.</param>
        internal CacheWorkflow(
            string uniqueId,
            IEnumerable<CacheTransition> backEndTransitions,
            IEnumerable<CacheTransition> schemaTransitions,
            SchemaBase schemaField,
            bool isStrictWorkflow,
            IEnumerable<string> backEndStates = null)
            : base(uniqueId, CacheIdentity.DefaultId, string.Empty, CacheIdentity.DefaultRevision)
        {
            PreCondition.AssertNotNull(backEndTransitions, "backEndTransitions");
            PreCondition.AssertNotNull(schemaTransitions, "schemaTransitions");
            PreCondition.AssertNotNull(schemaField, "schemaField");

            FieldSchema = schemaField;

            if (!schemaTransitions.IsEmpty() && FieldSchema.IsValidSchemaValueMapping(backEndStates))
            {
                // state allowed values field is overloaded. this means the transitions from
                // the back-end system aren't valid anymore because they
                // are overwritten in the associated schema file. now we have
                // to initialize the transition list and fill it with the
                // possible transitions.
                IsStrict = false;

                _transitions = new CacheElementList<CacheTransition>(
                    InitializeTransitions(schemaTransitions),
                    this);
            }
            else
            {
                IsStrict = isStrictWorkflow;

                _transitions = new CacheElementList<CacheTransition>(
                    InitializeTransitions(backEndTransitions),
                    this);
            }
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        private IList<CacheTransition> InitializeTransitions(IEnumerable<CacheTransition> transitions)
        {
            IList<CacheTransition> transToInit = new List<CacheTransition>();

            transitions.ForEach(
                t =>
                {
                    if (string.IsNullOrEmpty(t.From))
                    {
                        TransitionStart = t.To;
                    }
                    else if (string.IsNullOrEmpty(t.To))
                    {
                        TransitionEnd = t.From;
                    }
                    else if (!string.IsNullOrEmpty(t.From) && !string.IsNullOrEmpty(t.To))
                    {
                        transToInit.Add(t);
                    }
                });
            return transToInit;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}